//
//  AlarmListViewController.m
//  StormWatch
//
//  Created by Hero on 2024/9/5.
//

#import "AlarmListViewController.h"
#import "AlarmListTableViewCell.h"
#import "AlarmEditView.h"


@interface AlarmListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic, strong) CBPeripheral * peripheral;

@end

@implementation AlarmListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jy_title = SWString(@"Alarm");
    NSString * uuid = self.metaInfo[@"uuid"];
    NSArray * list = [FRQBluetoothManager shareManager].curPeripherals;
    for (CBPeripheral * per in list) {
        if ([per.identifier.UUIDString isEqualToString:uuid]) {
            self.peripheral = per;
        }
    }
    
    [self.view addSubview:self.tableView];
    
    [self initData];
    // Do any additional setup after loading the view.
}

- (void)initData {
    [[WatchCommandTool sharedInstance] readStormCommand:Command_Alarm ToPeripheral:self.peripheral reBack:^(NSString * _Nonnull backData) {
        if (backData) {
            //  54590101800025001502090011 00 0100010000 0200010000 0300010000 00
            NSString * valueString = [backData substringFromIndex:14*2];
            long a =  (valueString.length - 2) / 10;
            NSString * string = [backData substringWithRange:NSMakeRange(14 * 2, 5 * a * 2)];
            for (int i = 0; i < a; i ++) {
                NSString * info = [string substringWithRange:NSMakeRange(10 * i , 10)];
                AlermModel * m = [[AlermModel alloc] initWithString:info];
                [self.dataArr addObject:m];
            }
            [self.tableView reloadData];
        }
    }];
}

- (void)initUI {
    [self.tableView registerNib:[UINib nibWithNibName:@"AlarmListTableViewCell" bundle:nil] forCellReuseIdentifier:@"AlarmListTableViewCell"];
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AlarmListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AlarmListTableViewCell" forIndexPath:indexPath];
    cell.model = self.dataArr[indexPath.row];
    cell.changeBlock = ^(AlermModel * _Nonnull model) {
        [self writeAlerm:model];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AlarmEditView * alert = [[AlarmEditView alloc] initWithAlertModel:self.dataArr[indexPath.row]];
    alert.sureBlock = ^(AlermModel * _Nonnull model) {
        [self writeAlerm:model];
    };
    [alert show];
}

- (void)writeAlerm:(AlermModel *)model {
    [[WatchCommandTool sharedInstance] writeAlermInfo:model   CallBack:^(BOOL success) {
        if (success) {
            [self.tableView reloadData];
        }
    } ToPeripheral:self.peripheral];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.jy_bar_h, kScreenWidth, kScreenHeight - kNavigationBarHeight - kHomeIndicatorHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;

//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerNib:[UINib nibWithNibName:@"AlarmListTableViewCell" bundle:nil] forCellReuseIdentifier:@"AlarmListTableViewCell"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedRowHeight = 40;
        } else {
            // Fallback on earlier versions
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.scrollsToTop =  NO;
        
        if (@available(iOS 15, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        
    }
    return _tableView;
}


- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
